namespace :rc do
  desc "Populates store with sample products"
  task :sample_data => :environment do
    require 'active_record/fixtures'
    require 'custom_fixtures'

    # load initial database fixtures (in db/sample/*.yml) into the current environment's database
    ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
    Dir.glob(File.join(RAILS_ROOT, 'db', 'sample', '*.{yml,csv}')).each do |fixture_file|
      Fixtures.create_fixtures('db/sample', File.basename(fixture_file, '.*'))
    end
    
    # make product images available to the app
    target = "#{RAILS_ROOT}/public/images/products/"
    source = "#{RAILS_ROOT}/lib/tasks/sample/images/"
    FileUtils.mkdir_p target
    #File.makedirs target
    
    Dir.new(source).each do |filename|
      if not (filename == '.' or filename == '..' or filename == '.svn')
        src = source + "#{filename}"
        dst = target + "#{filename}"
        FileUtils.cp src, dst
      end
      #copy filename, target, true
      #puts "Inside /usr/bin is something called #{filename}"
    end
    puts "Sample products have been loaded into to the store"
  end
end

